﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///系统导航菜单
    /// </summary>
    public partial class navigation
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig(); //获得系统配置信息
        private readonly DAL.navigation dal;
        private readonly DAL.Data data;

        public navigation()
        {
            data = new DAL.Data();
            dal = new DAL.navigation();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_navigation>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_navigation model)
        {
            return data.Insert(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_navigation model)
        {
            return data.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return data.Delete<Entitys.dt_navigation>(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_navigation GetModel(int id)
        {
            return data.Get<Entitys.dt_navigation>(id);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_navigation> GetList(int Top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_navigation>(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_navigation> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_navigation>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 查询名称是否存在
        /// </summary>
        public bool Exists(string name)
        {
            return data.Exists<Entitys.dt_navigation>(x=>x.name==name);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_navigation GetModel(string name)
        {
            return data.Get<Entitys.dt_navigation>(x=>x.name==name);
        }

        /// <summary>
        /// 取得所有列表
        /// </summary>
        public List<Entitys.dt_navigation> GetList(int parent_id, string nav_type)
        {
            //return data.List<Entitys.dt_navigation>(x => x.nav_type == nav_type);
            return dal.GetList(parent_id, nav_type);
        }

        /// <summary>
        /// 根据导航的名称查询其ID
        /// </summary>
        /// <param name="nav_name">菜单名称</param>
        /// <returns>int</returns>
        public int GetNavId(string nav_name)
        {
            return data.Get<Entitys.dt_navigation>(x => x.name == nav_name).id;
        }

        /// <summary>
        /// 快捷添加系统默认导航
        /// </summary>
        /// <param name="parent_name">父导航名称</param>
        /// <param name="nav_name">导航名称</param>
        /// <param name="title">导航标题</param>
        /// <param name="link_url">链接地址</param>
        /// <param name="sort_id">排序数字</param>
        /// <param name="channel_id">所属频道ID</param>
        /// <param name="action_type">操作权限以英文逗号分隔开</param>
        /// <returns>int</returns>
        public int Add(string parent_name, string nav_name, string title, string link_url, int sort_id, int channel_id, string action_type)
        {
            Entitys.dt_navigation parentModel = data.Get<Entitys.dt_navigation>(x => x.name == parent_name);
            if (parentModel != null)
            {

                Entitys.dt_navigation model = new Entitys.dt_navigation();
                model.action_type = action_type;
                model.channel_id = channel_id;
                model.is_lock = 0;
                model.is_sys = 1;
                model.link_url = link_url;
                model.name = nav_name;
                model.nav_type = DTEnums.NavigationEnum.System.ToString();
                model.parent_id = parentModel.id;
                model.sort_id = sort_id;
                model.title = title;
                return data.Insert(model);
            }
            else
                return 0;
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(Expression<Func<Entitys.dt_navigation, Entitys.dt_navigation>> expression, Expression<Func<Entitys.dt_navigation, bool>> whereExpression)
        {
            return data.UpdateColumns<Entitys.dt_navigation>(expression, whereExpression);
        }
        #endregion
    }
}